package com.paiai.lslease.service.appinvite;

import com.paiai.lslease.auto.dao.AppInvitationDAO;
import com.paiai.lslease.auto.model.AppInvitation;
import com.paiai.lslease.auto.model.AppInvitationExample;
import com.paiai.lslease.auto.model.AppLockTenant;
import com.paiai.lslease.auto.model.AppSysUser;
import com.paiai.lslease.dao.apphouse.AppHouseDiyDAO;
import com.paiai.lslease.dao.appinvitation.AppInvitationExternalDAO;
import com.paiai.lslease.model.AppInvitationInfo;
import com.paiai.lslease.service.apphouse.AppLockTenantService;
import com.paiai.lslease.service.appuser.AppSysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

public class AppInviteServiceImpl implements AppInviteService {

    private static final Logger logger = LoggerFactory.getLogger(AppInviteServiceImpl.class);

    private AppInvitationDAO appInvitationDAO;
    private AppInvitationExternalDAO appInvitationExternalDAO;
    private AppLockTenantService appLockTenantService;
    private AppSysUserService appSysUserService;
    private AppHouseDiyDAO appHouseDiyDAO;

    public void setAppInvitationDAO(AppInvitationDAO appInvitationDAO) {
        this.appInvitationDAO = appInvitationDAO;
    }

    public void setAppInvitationExternalDAO(AppInvitationExternalDAO appInvitationExternalDAO) {
        this.appInvitationExternalDAO = appInvitationExternalDAO;
    }

    public void setAppLockTenantService(AppLockTenantService appLockTenantService) {
        this.appLockTenantService = appLockTenantService;
    }

    public void setAppSysUserService(AppSysUserService appSysUserService) {
        this.appSysUserService = appSysUserService;
    }

    public void setAppHouseDiyDAO(AppHouseDiyDAO appHouseDiyDAO) {
        this.appHouseDiyDAO = appHouseDiyDAO;
    }

    public AppInvitation sendRentingInvitation(AppInvitation appInvitation) throws Exception {
        appInvitationDAO.insert(appInvitation);
        return selectSingleInvitation(appInvitationExternalDAO.getUpToDateAppInvitation());
    }

    public List<AppInvitationInfo> selectUserRentingInvitationList(String userMobile) throws Exception {
        return appHouseDiyDAO.selectRentingInvitationListByMobile(userMobile);
    }

    public boolean confirmRentingInvitationByPrimaryId(AppInvitation appInvitation) throws Exception {
        //更新邀请记录状态为已接受邀请
        appInvitationDAO.updateByPrimaryKey(appInvitation);
        AppSysUser appSysUser = appSysUserService.getAppSysUserInfoByMobile(appInvitation.getUserMobile() + "");
        //将该租客之前的租住记录删除
        appLockTenantService.deleteLockTenant(appSysUser.getId(), appInvitation.getLockId());
        //保存用户租房信息信息到租客的设备关联表
        AppLockTenant appLockTenant = new AppLockTenant();
        appLockTenant.setTenantId(appSysUser.getId());
        appLockTenant.setLockId(appInvitation.getLockId());
        appLockTenant.setCheckDate(appInvitation.getSendInviteTime());
        appLockTenant.setDeadDate(appInvitation.getRentingEndTime());
        appLockTenant.setRentDays((int) ((appInvitation.getRentingEndTime().getTime() - appInvitation.getSendInviteTime().getTime()) / (1000 * 60 * 60 * 24)));
        appLockTenant.setUserLockName("租客" + appSysUser.getMobile().substring(appSysUser.getMobile().length() - 4) + "的新设备");
        appLockTenant.setItem1("1");
        appLockTenant.setItem2("1");
        appLockTenant.setUpdateTime(new Date());
        appInvitation.setInsertTime(new Date());
        //将用户角色更新为租客
        return appSysUserService.addTenantRoleToUserByMobile(appInvitation.getUserMobile()) && appLockTenantService.insertLockTenant(appLockTenant) != null;
    }

    public AppInvitation selectSingleInvitation(int invitationId) throws Exception {
        AppInvitation appInvitation = appInvitationDAO.selectByPrimaryKey(invitationId);
        return appInvitation != null && "1".equalsIgnoreCase(appInvitation.getItem1()) ? appInvitation : null;
    }

    public boolean existInvitation(int invitationId) throws Exception {
        return selectSingleInvitation(invitationId) != null;
    }

    @Override
    public boolean existNotAcceptInvitation(int landlordId, String userMobile, int lockId) throws Exception {
        AppInvitationExample appInvitationExample = new AppInvitationExample();
        appInvitationExample.createCriteria().andLandlordIdEqualTo(landlordId).andUserMobileEqualTo(userMobile).andLockIdEqualTo(lockId).andItem1EqualTo("1").andStateEqualTo(1);
        List<AppInvitation> appInvitationList = appInvitationDAO.selectByExample(appInvitationExample);
        return appInvitationList != null && appInvitationList.size() > 0;
    }

    @Override
    public AppInvitation updateRentingInvitation(AppInvitation appInvitation) throws Exception {
        appInvitationDAO.updateByPrimaryKey(appInvitation);
        return appInvitationDAO.selectByPrimaryKey(appInvitation.getLockId());
    }
}
